﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ReportPrinting;

namespace BSE.Windows.Test
{
    public partial class TenDaysForm : BaseForm, IReportMaker
    {
        private bool initCompleted = false;

        public TenDaysForm()
        {
            InitializeComponent();
            InitToolStrip();
        }

        private void TenDaysForm_Load(object sender, EventArgs e)
        {
            RefreshForm();
            initCompleted = true;
        }

        private DateTime SelectDate(DateTime d)
        {
            if (d.Day >= 25)
                d = new DateTime(d.Year, d.Month, 25);
            else if (d.Day >= 15)
                d = new DateTime(d.Year, d.Month, 15);
            else if (d.Day >= 5)
                d = new DateTime(d.Year, d.Month, 5);
            else
            {
                d = d.AddMonths(-1);
                d = new DateTime(d.Year, d.Month, 25);
            }
            return d;
        }

        private void InitToolStrip()
        {
            int year = DateTime.Now.Year;
            toolStripComboBoxYear.ComboBox.Items.Clear();
            for (int i = 0; i < 10; i++)
                toolStripComboBoxYear.ComboBox.Items.Add((year + 1 - i).ToString());
            toolStripComboBoxYear.SelectedIndex = 1;
            toolStripComboBoxYear.SelectedText = toolStripComboBoxYear.Items[1].ToString();
            int month = DateTime.Now.Month;
            toolStripComboBoxMonth.ComboBox.Items.Clear();
            for (int i = 1; i < 12; i++)
            {
                toolStripComboBoxMonth.ComboBox.Items.Add(i.ToString());
            }
            toolStripComboBoxMonth.ComboBox.SelectedIndex = month - 1;
            toolStripComboBoxMonth.ComboBox.SelectedText = toolStripComboBoxMonth.ComboBox.Items[month - 1].ToString();

            toolStripComboBoxDay.ComboBox.Items.Clear();
            toolStripComboBoxDay.ComboBox.Items.Add("5");
            toolStripComboBoxDay.ComboBox.Items.Add("15");
            toolStripComboBoxDay.ComboBox.Items.Add("25");
            toolStripComboBoxDay.ComboBox.SelectedIndex = 0;
            toolStripComboBoxDay.ComboBox.SelectedText = toolStripComboBoxDay.ComboBox.Items[0].ToString();

        }

        public override void RefreshForm()
        {
            if (!initCompleted)
                return;

            LoadSummaryList();
        }

        private void LoadSummaryList()
        {
            int year = DateTime.Now.Year + 1 - toolStripComboBoxYear.SelectedIndex;
            int month = toolStripComboBoxMonth.SelectedIndex + 1;
            int day = toolStripComboBoxDay.SelectedIndex * 10 + 5 ;
            DateTime end = new DateTime(year, month, day);

            DateTime start = end;
            if (end.Day == 25 || end.Day == 15)
                start = end.AddDays(-9);
            else if (end.Day == 5)
            {
                start = end.AddMonths(-1);
                start = new DateTime(start.Year, start.Month, 26);
            }

            List<YaopinAccount> ya = AnesthesiaBLL.GetSummary(start, end);
            LoadSummaryList(ya);
           
        }

        private void LoadSummaryList(List<YaopinAccount> ya)
        {
            listView1.Items.Clear();
            int i = 1;

            foreach (YaopinAccount y in ya)
            {
                ListViewItem lvit = new ListViewItem(i.ToString());
                lvit.ImageIndex = 0;
                lvit.Tag = y;
                i++;

                if (i % 2 == 1)
                    lvit.BackColor = Color.FromArgb(255, 192, 255, 255);
                else
                    lvit.BackColor = Color.FromArgb(255, 255, 255, 128);
/*
                ListViewItem lvit = new ListViewItem(y.Yaopin.YaopinCode);
                lvit.Tag = y;
*/
                ListViewItem.ListViewSubItem lvsit = new ListViewItem.ListViewSubItem();
                lvsit.Text = y.Yaopin.YaopinName;
                lvit.SubItems.Add(lvsit);

                lvsit = new ListViewItem.ListViewSubItem();
                lvsit.Text = y.Yaopin.YaopinGuige;
                lvit.SubItems.Add(lvsit);

                lvsit = new ListViewItem.ListViewSubItem();
                lvsit.Text = y.Yaopin.YaopinDanwei;
                lvit.SubItems.Add(lvsit);
                
                lvsit = new ListViewItem.ListViewSubItem();
                lvsit.Text = y.SumUsageHospital.ToString();
                lvit.SubItems.Add(lvsit);

                lvsit = new ListViewItem.ListViewSubItem();
                lvsit.Text = y.SumUsageOutpatient.ToString();
                lvit.SubItems.Add(lvsit);

                lvsit = new ListViewItem.ListViewSubItem();
                lvsit.Text = y.SumUsageTotal.ToString();
                lvit.SubItems.Add(lvsit);

                listView1.Items.Add(lvit);
            }
        }

        private void toolStripComboBoxDrug_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!initCompleted)
                return;

            try
            {
                LoadSummaryList();
            }
            catch (Exception ex)
            {
                MessageBox.Show("读取数据失败！", "医院", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void MakeDocument(ReportDocument reportDocument)
        {
            TextStyle.ResetStyles();

            TextStyle.Normal.Size = 10;
            TextStyle.Heading1.Size = 16;
            TextStyle.Heading1.Bold = false;
            TextStyle.TableHeader.BackgroundBrush = Brushes.White;// Brushes.Blue;
            TextStyle.TableHeader.Brush = Brushes.Black;//Brushes.LightGoldenrodYellow;
            TextStyle.TableHeader.MarginNear = 0.1f;
            TextStyle.TableHeader.MarginFar = 0.1f;
            TextStyle.TableHeader.MarginTop = 0.05f;
            TextStyle.TableHeader.MarginBottom = 0.05f;
            TextStyle.TableRow.MarginNear = 0.1f;
            TextStyle.TableRow.MarginFar = 0.1f;
            TextStyle.TableRow.MarginTop = 0.05f;

            ReportBuilder builder = new ReportBuilder(reportDocument);
            builder.StartLinearLayout(Direction.Vertical);

            Pen innerPen = new Pen(Color.Green, 0.02f);

            int year = DateTime.Now.Year + 1 - toolStripComboBoxYear.SelectedIndex;
            int month = toolStripComboBoxMonth.SelectedIndex + 1;
            int day = toolStripComboBoxDay.SelectedIndex * 10 + 5;
            builder.AddPageHeader(String.Empty, "麻精药品统计旬报表\n" + year + " 年  " + month + " 月  " + day + " 日", "第 %p 页");
            //builder.AddPageHeader(String.Empty, "" + year + " 年  " + month + "月", String.Empty);

            //builder.AddText("Table with lines using default lines\n");
            // Following line sets up the pen used for lins for tables
            builder.DefaultTablePen = reportDocument.ThinPen;
            DataView dv = GetDataView();
            builder.AddTable(dv, true);
            builder.CurrentSection.HorizontalAlignment = ReportPrinting.HorizontalAlignment.Center;
            builder.AddColumn("YaopinName", "药品名称", 3.6f, true, true);
            builder.AddColumn("YaopinGuige", "规格", 2.0f, true, true);
            builder.AddColumn("YaopinDanwei", "单位", 1.8f, true, true);
            builder.AddColumn("SumUsageHospital", "住院使用", 1.8f, true, true);
            builder.AddColumn("SumUsageOutpatient", "门诊使用", 1.8f, true, true);
            builder.AddColumn("SumTotal", "合计", 1.8f, true, true);
            builder.Table.OuterPens = reportDocument.NormalPen;

            builder.FinishLinearLayout();
        }

        private DataView GetDataView()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("YaopinName");
            dt.Columns.Add("YaopinGuige");
            dt.Columns.Add("YaopinDanwei");
            dt.Columns.Add("SumUsageHospital");
            dt.Columns.Add("SumUsageOutpatient");
            dt.Columns.Add("SumTotal");

            foreach (ListViewItem item in listView1.Items)
            {
                YaopinAccount ya = item.Tag as YaopinAccount;
                DataRow dr = dt.NewRow();
                dr["YaopinName"] = ya.Yaopin.YaopinName;
                dr["YaopinGuige"] = ya.Yaopin.YaopinGuige;
                dr["YaopinDanwei"] = ya.Yaopin.YaopinDanwei;
                dr["SumUsageHospital"] = ya.SumUsageHospital;
                dr["SumUsageOutpatient"] = ya.SumUsageOutpatient;
                dr["SumTotal"] = ya.SumUsageTotal;
                dt.Rows.Add(dr);
            }

            return dt.DefaultView;
        }
    }
}
